﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FarseerGames.FarseerPhysics.Collisions;
using FreeGoo.GameEngine.Renderables.Links;

namespace FreeGoo.GameEngine.Renderables.Particles
{
    public class Balloon : Particle
    {
        // TODO - use force instead of link?
        public Balloon(Geom geom, float radius, float buoyancy)
            : base(geom, radius)
        {
            MaximumLinkTargets = 1;
            MinimumLinkTargets = 1;
            Buoyancy = buoyancy;
            LinkSoftnessFactor = 5;
            LinkBreakpointFactor = 5;
            MinAllowedLinkDistance = 0.1f;
            LinkableDistanceLinkBias = DefaultBalloonLinkableDistanceLinkBias;
        }

        public float Buoyancy { get; set; }

        public override bool AllowsLinkFrom(ILinkable linkable)
        {
            // Can't link _to_ a balloon, only from it
            return false;
        }

        public override void UpdatePhysics()
        {
            base.UpdatePhysics();
            if (IsLinked)
            {
                // TODO: Implement a balloon ceiling, so that balloons don't simply go off the top of the map,
                // but loose force the higher they go
                Body.ApplyForce(Engine.PhysicsSimulator.Gravity * -Buoyancy);
            }
        }
       
        public override void AddLink(Link link)
        {
            base.AddLink(link);
            Body.LinearDragCoefficient = 100;
        }

        public override void RemoveLink(Link link)
        {
            base.RemoveLink(link);
            if (!IsLinked)
            {
                Body.LinearDragCoefficient = DefaultParticleLinearDragCoefficient;
            }
        }

        protected override Link CreateActualLink(ILinkable first, ILinkable second)
        {
            return MixedFactory.CreateStringLinkBetweenLinkableObjects(Engine, first, second, this);
        }  
    }
}
